Thread: Mastermind game possibilities help [no answers please, just hints]

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Question Mastermind game possibilities help [no answers please, just hints]

    Hey guys, I'm in a cs1 class at my school and I need a little help. The program is a checker for the amount of possibilities in a game of Mastermind. Here is the code that I have come up with so far. Pretty sure the problem is in the function validGuess. If you guys see anything wrong, could you point out where it is and give a hint instead of the answer? I actually want to learn this so that would be awesome. Thank you (I also attached my .c file incase someone wanted to look at it like that)mastermind.c

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    int** scanArray(int rows, int cols);
    void freeArray(int** arr, int rows);
    int possible(int** curGame, int slots, int colors, int moves, int k, int* guess);
    int validGuess(int** curGame, int slots, int moves, int* guess);
    
    
    int main (){
        int games, slots, colors, moves;
        int i;
    
    
        scanf("%d", &games);
    
    
        for (i=0; i <games; i++){
            scanf("%d %d %d", &slots, &colors, &moves);
            int** curGame= scanArray(moves, slots+2);
    
    
            int* guess = malloc(sizeof(int)*slots);
    
    
            printf("%d\n", possible(curGame, slots, colors, moves, 0, guess));
    
    
            free(guess);
            freeArray(curGame, moves);
        }
    
    
        return 0;
    }
    
    
    int** scanArray(int rows, int cols){
        int** grid = malloc(sizeof(int*) * rows);
        int i, j;
        for (i=0; i<rows; i++){
            grid[i] = malloc(sizeof(int) * cols);
            for (j=0; j<cols; j++)
                scanf("%d", &grid[i][j]);
        }
        return grid;
    }
    
    
    void freeArray(int** arr, int rows){
        int i;
        for (i=0; i<rows; i++){
            free(arr[i]);
        }
        free(arr);
    }
    
    
    int possible(int** curGame, int slots, int colors, int moves, int k, int* guess){
        int i, sum=0;
    
    
        if(k == slots)
            return validGuess(curGame, slots, moves, guess);
    
    
        else {
            for(i=0; i<colors; i++){
                guess[k] = i;
                sum+= possible(curGame, slots, colors, moves, k+1, guess);
            }
        }
        return sum;
    }
    
    
    int validGuess(int** curGame, int slots, int moves, int* guess){
        int black, white, i, j, k;
        int* used = (int*)calloc(slots, sizeof(int));
    
    
        for (i=0; i<moves; i++){
            black= 0;
            white= 0;
            //for (j=0; j<slots; j++)
                //used[j]= 0;
    
    
            for (j=0; j<slots; j++){
                if (curGame[i][j] == guess[j]){
                    black++;
                    used[j] = 1;
                }
            }
            if (black != curGame[i][slots+1])
                return 0;
            for (j=0; j<slots; j++){
                if (used[j] == 1){
                    continue;
                }
                for (k=0; k<slots; k++){
                    if (used[k] == 1){
                        continue;
                    }
                    if (curGame[i][j] == guess[k]){
                        white++;
                        break;
                    }
    
    
                }
                if (white != curGame[i][slots+2])
                    return 0;
            }
    
    
        }
        return 1;
    }
    and here is the actual problem that the Professor typed out :Your task will be to analyze a partially completed game and calculate the number of possiblecolor combinations that are completely consistent with all of the given information. Due to thefact that the manufacturer may release different versions of the game with a different number ofpossible colors and a different number of possible slots, your program should work where thesetwo values are given as variables
    Last edited by TheWhiteCuban; 10-03-2015 at 05:14 PM. Reason: bad attachment

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Do you have any test data?
    And maybe even the result you expect from that test data.

    Looking at code is fine, but to be able to run the code, we need some idea of what data to enter and what we're expecting to see as a result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    2
    Sorry about that, the test data is

    INPUT
    3
    4 6 4
    0 1 2 3 0 2
    2 2 4 1 0 2
    4 3 3 2 1 1
    1 3 5 4 1 3
    6 10 3
    0 1 2 3 4 5 0 0
    7 7 8 8 9 9 0 4
    9 9 8 8 7 7 4 0
    10 2 1
    0 1 0 1 0 1 0 1 0 1 5 4

    OUTPUT
    1
    36
    200

    the first input is the number of games being tested, the line of three before each game is the number of slots, colors possible, and moves. The last two numbers in each longer line of numbers are the number of blacks and whites respectively. Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program for the mastermind game
    By College94 in forum C Programming
    Replies: 3
    Last Post: 04-14-2015, 12:09 PM
  2. Again, Mastermind Game Assistant Question
    By Jesterira in forum C Programming
    Replies: 8
    Last Post: 12-07-2012, 06:45 PM
  3. Problem with simple Mastermind game(beginner)
    By newbie2012 in forum Game Programming
    Replies: 1
    Last Post: 11-12-2012, 03:51 AM
  4. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM
  5. Replies: 37
    Last Post: 12-13-2007, 03:40 PM

Tags for this Thread